﻿using Newtonsoft.Json;
using System;
using System.Collections;
using Yunzuan.Common;
using Yunzuan.Common.Log;
using Yunzuan.Foundation.DataAccess;
using YunZuan.Models.Entities;
using Yunzuan.Foundation.Serializer;
using Yunzuan.Foundation.Web_api;
using YunZuan.Models;

namespace RetailService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“SubRetail”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 SubRetail.svc 或 SubRetail.svc.cs，然后开始调试。
    public class SubRetail : ISubRetail
    {
        #region 全局设置
        /// <summary>
        /// 全局设置
        /// </summary>
        /// <param name="tType"></param>
        /// <param name="GoodtypeId"></param>
        /// <returns></returns>
        public static SysSettingInfo GetSystemSetting(int tType, int GoodtypeId, string tttagKey)
        {
            SysSettingInfo setting = new SysSettingInfo();
            Hashtable hash = new Hashtable();
            hash.Add("SysSettingID", GoodtypeId);
            setting = YZ.GetDatabase(tttagKey).QueryForObject<SysSettingInfo>("Syssetting.GetSetting", hash);
            return setting;
        }
        #endregion
        #region 查裸钻
        /// <summary>
        /// 查裸钻
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public string QueryStock(string strParam)
        { 
            string tttagKey = "";
            string _msgInfo = "{\"msgcode\":#code,\"msginfo\":\"#msg\",\"data\":#result}";
            try
            {
                HashParam str = JsonConvert.DeserializeObject<HashParam>(strParam);
                tttagKey = CommonUrl.getURL(str, strParam, "查询库存"); 

                if (str.ContainsKey("PublicParentUrl") && str.ContainsKey("ParentKey"))
                {
                     
                    var publicParentUrl = str["PublicParentUrl"].ToString(); //不要把这些信息带到零售上一级系统
                    var ParentKey = str["ParentKey"].ToString();
                    str["TagKey"] = tttagKey;
                    str.Remove("PublicParentUrl");
                    str.Remove("ParentKey"); 

                    var inventoryJson = JsonConvert.SerializeObject(str);
                    var responseJson = MyHttpRequest.HttpRequest(string.Format("{0}Api/Open/QueryStock/?token={1}", publicParentUrl, ParentKey), inventoryJson);
                    return responseJson;                }
                else
                {
                    return _msgInfo.Replace("#code", "false").Replace("#msg", "获取库存失败:参数不正确").Replace("#result", JsonConvert.SerializeObject(""));
                }

            }
            catch (Exception ex)
            { 
                //日志：ex.Message
                return _msgInfo.Replace("#code", "false").Replace("#msg", "获取库存失败:数据中心接口繁忙").Replace("#result", JsonConvert.SerializeObject(""));
            }
        }
        #endregion 
        #region 查其他商品
        /// <summary>
        /// 查其他商品
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public string QueryProductData(string strParam)
        {  
            try
            {
                HashParam hsParam = Json.Deserialize<HashParam>(strParam);
                string tttagKey = CommonUrl.getURL(hsParam, strParam);
                  
                
                if (hsParam.ContainsKey("PublicParentUrl") && hsParam.ContainsKey("ParentKey"))
                {
                    { 
                        var publicParentUrl = hsParam["PublicParentUrl"].ToString(); //不要把这些信息带到一级批发系统
                        var ParentKey = hsParam["ParentKey"].ToString();
                        hsParam["TagKey"] = tttagKey; 
                        hsParam.Remove("PublicParentUrl");
                        hsParam.Remove("ParentKey");
                        strParam = JsonConvert.SerializeObject(hsParam);  

                        if (!hsParam.ContainsKey("GoodsType"))
                        {
                            return CommonLog.MsgErrorInfo("未指定商品类别");
                        }
                        else
                        {
                            strParam = JsonConvert.SerializeObject(hsParam);
                            var pageIndex = hsParam.GetInt("pageIndex")??1 ;
                            var pageSize = hsParam.GetInt("pageSize")??50;
                            var responseJson = MyHttpRequest.HttpRequest(string.Format("{0}Api/Open/QueryProductData/?token={1}&pageIndex={2}&pageSize={3}", publicParentUrl, ParentKey, pageIndex, pageSize), strParam);
                             
                            return responseJson;
                        }
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return CommonLog.MsgErrorInfo("error" + ex.Message);
            }
        }
        #endregion

        #region 提交订单
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public string SubmitOrdersFromSub(string strParam)
        { 
            string _msgInfo = "{\"msgcode\":#code,\"msginfo\":\"#msg\",\"data\":#result}";
            try
            {
                HashParam str = JsonConvert.DeserializeObject<HashParam>(strParam); 

                if (str.ContainsKey("PublicParentUrl") && str.ContainsKey("ParentKey"))
                {

                    var publicParentUrl = str["PublicParentUrl"].ToString(); //不要把这些信息带到零售上一级系统
                    var ParentKey = str["ParentKey"].ToString(); 
                    str.Remove("PublicParentUrl");
                    str.Remove("ParentKey");

                    var inventoryJson = JsonConvert.SerializeObject(str);
                    var responseJson = MyHttpRequest.HttpRequest(string.Format("{0}Api/Open/SubmitOrdersFromSub/?token={1}", publicParentUrl, ParentKey), inventoryJson);
                    return responseJson;
                }
                else
                {
                    return _msgInfo.Replace("#code", "false").Replace("#msg", "获取库存失败:参数不正确").Replace("#result", JsonConvert.SerializeObject(""));
                }

            }
            catch (Exception ex)
            {
                //日志：ex.Message
                return _msgInfo.Replace("#code", "false").Replace("#msg", "获取库存失败:数据中心接口繁忙").Replace("#result", JsonConvert.SerializeObject(""));
            }
        }
        #endregion

    }
}
